﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Text;
using System.Collections.Generic;

public partial class Admin_adminNewClass : basePage
{
    newClass nc = new newClass();
    newClassBll ncBLL = new newClassBll();
    NewsBll newsBll = new NewsBll();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["action"]!=null)
            {
                string action = Request.QueryString["action"].ToString();
                string id = Request["id"].ToString();
                switch (action.Trim())
                {
                    case "delAll":
                        dellAll(id);
                        break;
                    case "editAll":
                        editAll(id);
                        break;                   
                    case "Insert":
                        Insert();
                        break;
                    default:
                        break;
                }
            }
            
        }
    }
    #region 显示最新的数据---郑同飞
    /// <summary>
    /// 显示最新的数据---郑同飞
    /// </summary>
    /// <param name="ParentID"></param>
    /// <param name="length"></param>
    /// <param name="sb"></param>
    /// <returns></returns>
    public string bindNewsClassList(int ParentID, int length, StringBuilder sb)
    {
        IList<newClass> list=ncBLL.GetModel();
        length++;
        foreach (newClass nc in list)
        {           
            sb.Append("<table class='tb2'><tr align='center'>");
            sb.Append("<td  align='left' width='20%'>" + "<input type='checkbox' name='m_id' value='" + nc.newClassId + "'  class='checkbox'  id='" + nc.newClassId + "' />" + nc.newClassId + ".<a href='adminNew.aspx?ClassId=" + nc.newClassId + "'>" + nc.newClassName + "</a>(<font id=\"count\""+nc.newClassId+" color='red'>" + countByClass(nc.newClassId) + "</font>)</td>");
            sb.Append("<td width='14%'><input size='13' type='text' name='ncname" + nc.newClassId + "' value='" +nc.newClassName+ "' ></td>");
            sb.Append("<td width='10%'><input size='10' type='text' name='ncTag" + nc.newClassId + "' value='" + nc.newTag+ "'></td>");
            sb.Append("<td width='16%'><input size='12' type='text' name='ncinfo" +  nc.newClassId + "' value='" + nc.newClassInfo + "'></td>");
            sb.Append("<td width='13%'><input type='button' value='删除' onClick=\"if(confirm('确定要删除吗')){$('#loading').show();ajax_get('?action=delAll&id='+this.title+'&count='+$('#count"+nc.newClassId+"'),'',function(msg){$('#cpcontainer').empty().append(msg);$('#loading').hide();});}else{return false}\" name='Input' class='btn' title='" + nc.newClassId + "'/> </td>");
            sb.Append("</tr></table>");
        }
        return sb.ToString();
    }
#endregion

    #region 统计该类别下的新闻数量---郑同飞
    /// <summary>
    /// 统计该类别下的新闻数量---郑同飞
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public string countByClass(object id)
    {
        if (id != null)
        {
            int i = Convert.ToInt32(id);
            return newsBll.countByClass(i).ToString();
        }
        else 
        {
            return null;
        }
    }
    #endregion

    #region ajax批量删除，sql拼接需要过滤的地方---郑同飞
    /// <summary>
    /// ajax批量删除，sql拼接需要过滤的地方---郑同飞
    /// </summary>
    /// <param name="id"></param>
    public void dellAll(string id) 
    {
        if (id.Contains(","))
        {
            string[] idParam = id.Split(',');
            string[] ncId = new string[idParam.Length];
            for (int i = 0; i < idParam.Length; i++)
            {
                //if (ncBLL.Exists())
                //{

                //}
                ncId[i] = idParam[i];
            }
            if (ncBLL.Delete(ncId))
            {

            }
        }
        else 
        {
            if (ncBLL.Delete(Convert.ToInt32(id)))
            {
                
            }
        }

    }
    #endregion

    #region ajax批量修改---郑同飞
    public void editAll(string id)
    {
        try
        {
            if (id.Contains(","))
            {
                string[] idParam = id.Split(',');
                for (int i = 0; i < idParam.Length; i++)
                {
                    nc.newClassId = Convert.ToInt32(idParam[i]);
                    nc.newClassName = Request.Form["ncname" + idParam[i] + ""].ToString();
                    nc.newClassInfo = Request.Form["ncinfo" + idParam[i] + ""].ToString();
                    nc.newTag = Request.Form["ncTag" + idParam[i] + ""].ToString();
                    if (ncBLL.Update(nc))
                    {

                    }
                    else
                        return;
                }        
            }
            else 
            {
                nc.newClassId = Convert.ToInt32(id);
                nc.newClassName = Request.Form["ncname" + id + ""].ToString();
                nc.newClassInfo = Request.Form["ncinfo" + id + ""].ToString();
                nc.newTag = Request.Form["ncTag" + id + ""].ToString();
                if (ncBLL.Update(nc))
                {

                }
                else
                    return;
            }
        }
        catch (Exception)
        {
            
            throw;
        }
    }
    #endregion   

    #region 添加新闻分类---郑同飞
    public void Insert()
    {
        nc.newClassName = Request.Form["ncname"].ToString();
        nc.newClassInfo = Request.Form["ncinfo"].ToString();
        nc.newTag = Request.Form["ncTag"].ToString();
        if (ncBLL.Insert(nc))
        {
        }
        else
        {
            return;               
        }
           
    }
    #endregion
}
